//
//  ShouCangViewController.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ShouCangViewController.h"

@interface ShouCangViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@end

@implementation ShouCangViewController
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"收藏";
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    // 右边编辑按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
//    [[DataBaseManager shareInstance] openDB];
    [[ReadDBManager shareInstance] openDB];
    
//    self.dataArr = [[DataBaseManager shareInstance] select];
    self.dataArr = [[ReadDBManager shareInstance] select];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = [_dataArr[indexPath.row] title];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NewSelectedVC *selectVC = [[NewSelectedVC alloc] init];
//    selectVC.str = [_dataArr[indexPath.row] url_3w];
//    NSLog(@"%@", selectVC.str);
//    [self.navigationController pushViewController:selectVC animated:YES];
    ReadSelectVC *selectVC = [[ReadSelectVC alloc] init];
    selectVC.str = [_dataArr[indexPath.row] thumb];
    NSLog(@"%@", selectVC.str);
    [self.navigationController pushViewController:selectVC animated:YES];
    [selectVC release];
}
// 删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        // 同步删除数据库数据
//        NewModel *m = _dataArr[indexPath.row];
//        // 从数组中删除数据
//        [_dataArr removeObjectAtIndex:indexPath.row];
//        // 同步视图
//        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//        [[DataBaseManager shareInstance] deleteNewsWithtitle:m.title];
//    }
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 同步删除数据库数据
        ReadModel *m = _dataArr[indexPath.row];
        // 从数组中删除数据
        [_dataArr removeObjectAtIndex:indexPath.row];
        // 同步视图
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [[ReadDBManager shareInstance] deleteReadWithtitle:m.title];

    }
}
// 弹出delete
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
